# -*- coding: utf-8 -*-
from . import BaseController
from backend.libs import myjson as json
from backend.models import Group, Resource, GroupResource


class GroupListController(BaseController):

    def get(self):
        g = Group.objects
        group_name = self.get_argument('groupName', '')
        if group_name:
            g = g.filter(name__contains=group_name)

        disabled = self.int_argument('disabled', 0)
        g = g.filter(disabled=disabled).order_by('show_order')
        
        number_per_page = self.int_argument('pageSize', 100)
        paginator = self.pagination(g.all(), number_per_page=number_per_page, m2m_fields=[])

        return self.render_json(data=paginator)


class GroupOperatorsController(BaseController):

    def get(self):
        Group_id = self.int_argument('id', 0)
        r = Group.objects.filter(id=Group_id).first()
        if not r:
            return self.render_json(data={'operators': []})
        
        return self.render_json(data={'operators': [o.to_option_dict() for o in r.operators.all()]})


class GroupListAllController(BaseController):

    def get(self):
        Groups = Group.objects.order_by('show_order').all()
        data = {'Groups': [ r.to_dict() for r in Groups ]}
        return self.render_json(data=data)


class GroupResourcesController(BaseController):

    def get(self):
        id = self.int_argument('id', 0)
        if id <= 0:
            return self.render_json(code=1003, msg='角色ID错误')
        r = Group.objects.filter(id=id).first()
        data = []
        if r:
            data = [f.to_dict() for f in r.resources.filter(disabled=0).all()] if r else []

        return self.render_json(data={'resources':data})


class CreateGroupController(BaseController):

    def post(self):
        data = json.loads(self.request.body)
        if not data['name']:
            return self.render_json(code=1001, msg='角色名称不能为空')
        Group_id = data['id']

        Group = Group() if int(Group_id) <= 0 else Group.objects.filter(id=Group_id).first()
        Group.name = data['name']
        Group.description = data['description']
        Group.disabled = data['disabled']
        Group.show_order = data['show_order']
        Group.save()
        return self.render_json(data=Group.to_dict())


class CreateGroupResourceRelController(BaseController):

    def post(self):
        data = json.loads(self.request.body)
        if not data['resourceIds']:
            return self.render_json(code=1001, msg='至少选择一个资源')
        Group_id = data['GroupId']
        if int(Group_id) <= 0:
            return self.render_json(code=1001, msg='角色ID错误')

        Group = Group.objects.filter(id=Group_id).first()
        if not Group:
            return self.render_json(code=1001, msg='没有找到指定ID的角色数据')

        # 删除所有原来的权限
        GroupResource.objects.filter(Group_id=Group_id).all().delete()
        print('** ' * 50)
        print(data['resourceIds'])
        resource_list = Resource.objects.filter(id__in=data['resourceIds']).all()
        for r in resource_list:
            Group_resource = GroupResource()
            Group_resource.Group = Group
            Group_resource.resource = r
            Group_resource.save()
        return self.render_json()


class DeleteGroupController(BaseController):

    def post(self, Group_id):
        Group_id = int(Group_id)
        if Group_id > 0:
            Group = Group.objects.filter(id=Group_id).first()
            if Group:
                Group.delete()

        return self.render_json()
